<template>
  <div class="doctor-records">
    <div class="dashboard-header">
      <h2>病历管理</h2>
      <div class="header-actions">
        <el-input
          v-model="searchQuery"
          placeholder="搜索病历"
          class="search-input"
          size="large"
        >
          <template #append>
            <el-button type="primary" icon="Search">搜索</el-button>
          </template>
        </el-input>
        <el-button type="primary" icon="Plus" @click="createNewRecord">创建新病历</el-button>
      </div>
    </div>
    
    <el-table :data="medicalRecords" style="width: 100%" stripe>
      <el-table-column prop="recordId" label="病历ID" width="120" sortable />
      <el-table-column prop="patientName" label="患者姓名" width="120" />
      <el-table-column prop="date" label="日期" width="150" />
      <el-table-column prop="status" label="状态" width="120">
        <template #default="scope">
          <el-tag :type="statusTagType(scope.row.status)">
            {{ scope.row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="department" label="科室" width="120" />
      <el-table-column prop="diagnosis" label="诊断结果" />
      <el-table-column prop="blockHash" label="区块链哈希" min-width="180" show-overflow-tooltip>
        <template #default="scope">
          <el-link type="primary" @click="viewBlockDetails(scope.row)">
            {{ truncateHash(scope.row.blockHash) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" icon="View" @click="viewRecordDetails(scope.row)"></el-button>
          <el-button type="success" size="small" icon="Edit" @click="editRecord(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <div class="pagination">
      <el-pagination
        v-model:current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        layout="prev, pager, next, jumper, sizes, total"
        background
      />
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'

export default {
  setup() {
    const searchQuery = ref('')
    const currentPage = ref(1)
    const pageSize = ref(10)
    const total = ref(50)
    
    const medicalRecords = ref([
      { recordId: 'REC20230721001', patientName: '张明', date: '2023-07-21 10:30', status: '已完成', department: '内科', diagnosis: '高血压II期', blockHash: '0x9b7b0d1e4d8f0c3a6b5d2e8f1a0c3b5d2e8f1a0c3' },
      { recordId: 'REC20230720002', patientName: '李红', date: '2023-07-20 14:15', status: '待审核', department: '内分泌科', diagnosis: '糖尿病I型', blockHash: '0x742d35cc6634c0532925a3b844bc454e4438f44e' },
      { recordId: 'REC20230719003', patientName: '王建国', date: '2023-07-19 09:45', status: '已归档', department: '心血管科', diagnosis: '冠心病', blockHash: '0x5aafe7e8d70aae3a1c1b8f8c1e1a0c3b5d2e8f1a0' },
      { recordId: 'REC20230718004', patientName: '刘静', date: '2023-07-18 16:20', status: '已完成', department: '呼吸科', diagnosis: '哮喘急性发作', blockHash: '0x3dca034f7e8d70aae3a1c1b8f8c1e1a0c3b5d2e8' },
      { recordId: 'REC20230717005', patientName: '陈晓', date: '2023-07-17 11:10', status: '待完善', department: '消化科', diagnosis: '慢性胃炎伴糜烂', blockHash: '0x1e57997e8d70aae3a1c1b8f8c1e1a0c3b5d2e8f1' }
    ])
    
    const statusTagType = (status) => {
      const statusMap = {
        '已完成': 'success',
        '待审核': 'warning',
        '已归档': 'info',
        '待完善': 'danger'
      }
      return statusMap[status] || ''
    }
    
    const truncateHash = (hash) => {
      if (!hash) return ''
      return `${hash.substring(0, 6)}...${hash.substring(hash.length - 4)}`
    }
    
    const createNewRecord = () => console.log('创建新病历')
    const viewRecordDetails = (record) => console.log('查看病历详情:', record)
    const editRecord = (record) => console.log('编辑病历:', record)
    const viewBlockDetails = (record) => console.log('查看区块链详情:', record.blockHash)
    
    return {
      searchQuery,
      currentPage,
      pageSize,
      total,
      medicalRecords,
      statusTagType,
      truncateHash,
      createNewRecord,
      viewRecordDetails,
      editRecord,
      viewBlockDetails
    }
  }
}
</script>

<style scoped>
.doctor-records {
  padding: 20px;
}

.dashboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.header-actions {
  display: flex;
  gap: 10px;
}

.search-input {
  width: 300px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>